package com.example.shop.merchant.controller;

import com.example.shop.common.api.ResponseResult;
import com.example.shop.common.api.ServiceReturn;
import com.example.shop.common.api.ServiceStateCode;
import com.example.shop.common.enums.SessionAttributeKey;
import com.example.shop.goods.dto.*;
import com.example.shop.goods.service.BrowseGoodsService;
import com.example.shop.goods.service.EvaluationService;
import com.example.shop.goods.service.GoodsService;
import com.example.shop.logistics.dto.DeliveryBill;
import com.example.shop.logistics.service.LogisticsService;
import com.example.shop.pojo.Merchant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.util.List;


@RestController
@RequestMapping("/merchant/goods")
public class MerchantGoodsController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    EvaluationService evaluationService;
    @Autowired
    BrowseGoodsService browseGoodsService;
    @Autowired
    LogisticsService logisticsService;


    /**
     * 商家详细浏览商品
     * @param goodsId 详细浏览的商品id
     * @return 返回商品的详细信息
     */
    @GetMapping(value = "/browse/{goodsId}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<MerchantGoodsDetailBrowseInfo> detailBrowseGoods(HttpSession session,@PathVariable("goodsId")Long goodsId){
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<MerchantGoodsDetailBrowseInfo> result = browseGoodsService.merchantObtainDetailInfo(merchant,goodsId);
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }

    /**
     * 浏览商品
     * @param queryParam 浏览参数
     * @return 返回浏览商品的列表
     */
    @PostMapping(value = "/browse",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<List<GoodsBrowseInfo>> browseGoods(HttpSession session, @RequestBody @Validated @NotNull GoodsQueryParam queryParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        if (queryParam == null) {
            queryParam = GoodsQueryParam.builder().build();
        }
        queryParam.setMerchantId(merchant.getMerchantId());
        ServiceReturn<List<GoodsBrowseInfo>> result = browseGoodsService.obtainBrowseList(queryParam, null);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }


    //==============================|| 待移动到系统模块，这是系统的功能 ||============================================

    /**
     * 删除评价
     *
     * @param evaluationId 要删除评价的id
     * @return 返回删除评价的状态
     */
    //保证了evaluationId不为null
    @GetMapping(value = "/system/delete/evaluation/{evaluationId}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<Boolean> deleteEvaluation(@PathVariable("evaluationId") Long evaluationId) {
        ServiceReturn<Boolean> result = evaluationService.deleteEvaluation(evaluationId);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }
//======================================================================================================================

    /**
     * 创建商品
     * @param newGoodsInfo 创建的商品信息
     * @return 返回创建的商品的详细信息
     */
    @PostMapping("/save")
    public ResponseResult<MerchantGoodsDetailBrowseInfo> createGoods(HttpSession session, @Validated NewGoodsInfoParam newGoodsInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<MerchantGoodsDetailBrowseInfo> result = goodsService.saveGoods(merchant, newGoodsInfo);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }

    @GetMapping(value = "/delete/{goodsId}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<Boolean> deleteGoods(HttpSession session, @PathVariable("goodsId") long goodsId) {
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<Boolean> result = goodsService.deleteGoods(merchant, goodsId);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }

    @PostMapping("/add/goodsStyle")
    public ResponseResult<GoodsStyle> addGoodsStyle(HttpSession session, @Validated NewGoodsStyle newGoodsStyle, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<GoodsStyle> result = goodsService.addGoodsStyle(merchant, newGoodsStyle);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }

    @PostMapping("/modify/baseInfo")
    public ResponseResult<Boolean> modifyGoodsBaseInfo(HttpSession session, @Validated ModifyGoodBaseInfoParam modifyBaseInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<Boolean> result = goodsService.modifyGoodsBaseInfo(merchant, modifyBaseInfo);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }


    @PostMapping("/modify/goodsStyle")
    public ResponseResult<GoodsStyle> modifyGoodsStyle(HttpSession session, @Validated ModifyGoodsStyleParam modifyStyle, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<GoodsStyle> result = goodsService.modifyGoodsStyle(merchant, modifyStyle);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }

    @GetMapping(value = "/delete/goodsStyle/{goodsId}/{goodsClass}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<Boolean> deleteGoodsStyle(HttpSession session, @PathVariable("goodsId") long goodsId, @PathVariable("goodsClass") String goodsClass) {
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<Boolean> result = goodsService.deleteGoodsStyle(merchant, goodsId, goodsClass);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }

    @PostMapping("/add/describePicture/{goodsId}")
    public ResponseResult<List<String>> addDescribePicture(HttpSession session, @PathVariable("goodsId") long goodsId, List<MultipartFile> describePictureList) {
        if (describePictureList == null) {
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<List<String>> result = goodsService.addDescribePicture(merchant, goodsId, describePictureList);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }


    @GetMapping(value = "/delete/describePicture/{goodsId}/{describePictureName}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<Boolean> deleteDescribePicture(HttpSession session, @PathVariable("goodsId") long goodsId, @PathVariable("describePictureName") String describePictureName) {
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<Boolean> result = goodsService.deleteDescribePicture(merchant, goodsId, describePictureName);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }

    @PostMapping("/add/detailPicture/{goodsId}")
    public ResponseResult<List<String>> addDetailPicture(HttpSession session, @PathVariable("goodsId") long goodsId, List<MultipartFile> detailPictureList) {
        if (detailPictureList == null) {
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<List<String>> result = goodsService.addDetailPicture(merchant, goodsId, detailPictureList);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }

    @GetMapping(value = "/delete/detailPicture/{goodsId}/{detailPictureName}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<Boolean> deleteDetailPicture(HttpSession session, @PathVariable("goodsId") long goodsId, @PathVariable("detailPictureName") String detailPictureName) {
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<Boolean> result = goodsService.deleteDetailPicture(merchant, goodsId, detailPictureName);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }

    @PostMapping(value = "/delivery",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<Boolean> deliveryGoods(HttpSession session,@RequestBody @Validated DeliveryBill deliveryBill,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<Boolean> result = logisticsService.deliveryGoods(merchant, deliveryBill);
        return ResponseResult.auto(result.getStateCode(), result.getReturnData());
    }
}